import Vue from 'vue'
import Vuex from 'vuex'
import pageApi from "./pageApi";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // token: sessionStorage.token,
    token: "12312312321",
    userInfo: sessionStorage.userInfo
  },
  getters:{
    isLogin: state => {
      return state.token
    },
    userInfo: state => {
      return state.userInfo
    }
  },
  mutations: {
    setData(state, data) {
        sessionStorage.setItem('token', data.token);
        sessionStorage.setItem('userInfo', JSON.stringify(data.userInfo));
        state.token = data.token;
        state.userInfo = JSON.stringify(data.userInfo)
    },
    delData(state) {
        state.token = null;
        state.userInfo = null;
        sessionStorage.clear();
    }
  },
  actions: {
    login(context, payload) {
      return new Promise(resolve => {
          // 模拟登录成功，写入 token 信息
        console.log(payload);
          context.commit('setData', {
              token: payload.token,
              userInfo: payload
          });
          resolve()
      })
    },
    update(context, payload) {
      return new Promise(resolve => {
          // 模拟登录成功，写入 token 信息
          context.commit('setData', {
              token: payload.token,
              userInfo: payload
          });
          resolve()
      })
    },
    logout(context) {
      context.commit('delData')
    }
  },
  modules: {
    pageApi,
  },
})
